package cn.liit.weixin.core.basic.accesstoken;

import java.io.IOException;
import java.util.Calendar;

import name.zbq.bush.utils.DateUtil;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import cn.liit.weixin.core.config.Config;

import com.alibaba.fastjson.JSONObject;

/**
 * AccessToken提供类
 * @author boqi.zhang
 */
public class AccessTokenSupport {
	static final String APIURL = "https://api.weixin.qq.com/cgi-bin/token";
	/**
	 * 从微信服务器加载access_token 且此方法不对当前包外开放
	 * @param apiurl 微信获取access_token接口地址
	 * @param appid appid
	 * @param secret appsecret
	 * @return
	 */
	static AccessToken loadAccessToken(String apiurl,String appid,String secret){
		apiurl = String.format("%s?grant_type=client_credential&appid=%s&secret=%s", apiurl.trim(),appid.trim(),secret.trim());
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(apiurl);
		CloseableHttpResponse httpResponse = null;
		AccessToken accessToken = null;
		try {
			httpResponse = httpClient.execute(httpGet);
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if(statusCode == HttpStatus.SC_OK){
				HttpEntity httpEntity = httpResponse.getEntity();
				String entityString = EntityUtils.toString(httpEntity);
				JSONObject fromObject = JSONObject.parseObject(entityString);
				accessToken = AccessToken.getInstance();
				accessToken.setAccess_token(fromObject.get("access_token").toString());
				Integer expires_in = Integer.parseInt(fromObject.get("expires_in").toString());
				accessToken.setExpires_in(expires_in);
				accessToken.setIsloaded(true);//标记已经加载过
				accessToken.setExpires_date(DateUtil.getLater(DateUtil.getCurrentDate(), Calendar.SECOND, expires_in));//设置到期时间
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return accessToken;
	}
	/**
	 * 获取AccessToken对象
	 * @return
	 */
	public synchronized static AccessToken getAccessToken(){
		AccessToken accessToken = AccessToken.getInstance();
		//提前十分钟
		if(!accessToken.isIsloaded() || accessToken.getExpires_date().before(DateUtil.getLater(DateUtil.getCurrentDate(), Calendar.SECOND, 600))){
			loadAccessToken(APIURL, Config.appid, Config.appsecret);
		}
		return accessToken;
	}
	/**
	 * 获取access_token
	 * @return
	 */
	public static String getAccess_token(){
		AccessToken accessToken = getAccessToken();
		return accessToken.getAccess_token();
	}
	/**
	 * 获取expire_in
	 * @return
	 */
	public static Integer getExpire_in(){
		AccessToken accessToken = getAccessToken();
		return accessToken.getExpires_in();
	}
	
	private AccessTokenSupport(){
	}
	
}
